package com.firefly.admin.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.annotations.annotation.OperationLog;
import com.firefly.admin.common.enums.OperationModuleEnum;
import com.firefly.admin.common.enums.OperationTypeEnum;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.system.entity.FySystemMenu;
import com.firefly.admin.system.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.firefly.admin.system.entity.FySystemUser;
import com.firefly.admin.system.service.FySystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统管理员接口API")
@RestController
@RequestMapping("/system/user")
public class FySystemUserController {
    @Autowired
    private FySystemUserService fySystemUserService;

    /**
     * 分页查询系统管理员接口
     *
     * @param systemUserQueryVo 系统管理员查询条件
     * @return 管理员信息列表
     */
    @ApiOperation(value = "分页查询系统管理员接口", notes = "分页查询系统管理员接口", httpMethod = "GET")
    @PreAuthorize(value = "hasAuthority('system:user')")
    @GetMapping("/list")
    public Result<Page<FySystemUser>> getSystemUserList(SystemUserQueryVo systemUserQueryVo) {
        return fySystemUserService.getSystemUserList(systemUserQueryVo);
    }

    /**
     * 获取系统管理员信息接口
     *
     * @param userId 系统管理员ID
     * @return 系统管理员信息
     */
    @ApiOperation(value = "获取系统管理员信息接口", notes = "通过用户ID获取用户详细信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, value = "系统管理员ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("/{userId}")
    public Result<FySystemUser> getSystemUser(@PathVariable Long userId) {
        return fySystemUserService.getSystemUser(userId);
    }

    /**
     * 系统管理员信息新增接口
     *
     * @param fySystemUser 系统管理员信息
     * @return 新增结果
     */
    @ApiOperation(value = "系统管理员信息新增接口", notes = "系统管理员信息新增接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_USER, operateDesc = "管理员新增")
    @PreAuthorize(value = "hasAuthority('system:user:add')")
    @PostMapping("/add")
    public Result<FySystemUser> add(@RequestBody FySystemUser fySystemUser) {
        return fySystemUserService.add(fySystemUser);
    }

    /**
     * 系统管理员信息编辑接口
     *
     * @param fySystemUser 系统管理员信息
     * @return 编辑结果
     */
    @ApiOperation(value = "系统管理员信息编辑接口", notes = "系统管理员信息编辑接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_USER, operateDesc = "管理员编辑")
    @PreAuthorize(value = "hasAuthority('system:user:update')")
    @PostMapping("/update")
    public Result<FySystemUser> update(@RequestBody FySystemUser fySystemUser) {
        return fySystemUserService.update(fySystemUser);
    }

    /**
     * 系统管理员状态修改接口
     *
     * @param systemUserStatusUpdateVo 系统管理员状态编辑参数
     * @return 操作结果
     */
    @ApiOperation(value = "系统管理员状态修改接口", notes = "系统管理员状态修改接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_USER, operateDesc = "管理员修改状态")
    @PreAuthorize(value = "hasAuthority('system:user:update:status')")
    @PostMapping("/update/status")
    public Result<String> updateStatus(@RequestBody SystemUserStatusUpdateVo systemUserStatusUpdateVo) {
        return fySystemUserService.updateStatus(systemUserStatusUpdateVo);
    }

    /**
     * 系统管理员信息删除接口
     *
     * @param userIds 管理员ID
     * @return 删除结果
     */
    @ApiOperation(value = "系统管理员信息删除接口", notes = "系统管理员信息删除接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIds", required = true, value = "管理员ID列表", paramType = "query", dataType = "List")
    })
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_USER, operateDesc = "管理员删除")
    @PreAuthorize(value = "hasAuthority('system:user:delete')")
    @PostMapping("/delete")
    public Result<String> delete(Long[] userIds) {
        return fySystemUserService.delete(userIds);
    }

    /**
     * 系统管理员重置密码接口
     *
     * @param userId 管理员ID
     * @return 操作结果
     */
    @ApiOperation(value = "系统管理员重置密码接口", notes = "系统管理员重置密码接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, value = "管理员ID", paramType = "query", dataType = "Long")
    })
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_USER, operateDesc = "管理员重置密码")
    @PreAuthorize(value = "hasAuthority('system:user:reset:password')")
    @PostMapping("/reset/password")
    public Result<String> resetPassword(Long userId) {
        return fySystemUserService.resetPassword(userId);
    }

    /**
     * 系统管理员分配角色接口
     *
     * @param systemUserAssignRolesVo 分配角色信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统管理员分配角色接口", notes = "系统管理员分配角色接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_USER, operateDesc = "管理员分配角色")
    @PreAuthorize(value = "hasAuthority('system:user:assign:roles')")
    @PostMapping("/assign/roles")
    public Result<String> assignRoles(@RequestBody SystemUserAssignRolesVo systemUserAssignRolesVo) {
        return fySystemUserService.assignRoles(systemUserAssignRolesVo);
    }

    /**
     * 系统管理员用户名校验接口
     *
     * @param userName 管理员用户名
     * @param userId   管理员ID
     * @return 校验结果
     */
    @ApiOperation(value = "系统管理员用户名校验接口", notes = "系统管理员用户名校验接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", required = true, value = "管理员用户名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userId", required = true, value = "管理员ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("/name/check")
    public Result<String> userNameCheck(String userName, Long userId) {
        return fySystemUserService.userNameCheck(userName, userId);
    }

    /**
     * 系统管理员手机号校验接口
     *
     * @param mobile 管理员手机号
     * @param userId 管理员ID
     * @return 校验结果
     */
    @ApiOperation(value = "系统管理员手机号校验接口", notes = "系统管理员手机号校验接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", required = true, value = "管理员手机号", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userId", required = true, value = "管理员ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("/mobile/check")
    public Result<String> mobileCheck(String mobile, Long userId) {
        return fySystemUserService.mobileCheck(mobile, userId);
    }

    /**
     * 系统管理员邮箱校验接口
     *
     * @param email  管理员邮箱
     * @param userId 管理员ID
     * @return 校验结果
     */
    @ApiOperation(value = "系统管理员邮箱校验接口", notes = "系统管理员邮箱校验接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", required = true, value = "管理员邮箱", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userId", required = true, value = "管理员ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("/email/check")
    public Result<String> emailCheck(String email, Long userId) {
        return fySystemUserService.emailCheck(email, userId);
    }

    /**
     * 登录用户密码强度校验接口
     *
     * @return 校验结果
     */
    @ApiOperation(value = "登录用户密码强度校验接口", notes = "登录用户密码强度校验接口", httpMethod = "GET")
    @GetMapping("/password/strength/check")
    public Result<Integer> passwordStrengthCheck() {
        return fySystemUserService.passwordStrengthCheck();
    }

    /**
     * 登录用户密码校验接口
     *
     * @param loginPwd 登录密码
     * @return 校验结果
     */
    @ApiOperation(value = "登录用户密码强度校验接口", notes = "登录用户密码强度校验接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginPwd", required = true, value = "登录密码", paramType = "query", dataType = "String")
    })
    @GetMapping("/password/check")
    public Result<String> passwordCheck(String loginPwd) {
        return fySystemUserService.passwordCheck(loginPwd);
    }

    /**
     * 获取系统管理员已选角色接口
     *
     * @param userId 管理员ID
     * @return 查询结果
     */
    @ApiOperation(value = "获取系统管理员已选角色接口", notes = "获取系统管理员已选角色接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, value = "管理员ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("/role")
    public Result<List<Long>> getUserRoles(Long userId) {
        return fySystemUserService.getUserRoles(userId);
    }

    /**
     * 系统管理员密码修改接口
     *
     * @param systemLoginPwdUpdateVo 系统管理员密码编辑参数
     * @return 操作结果
     */
    @ApiOperation(value = "系统管理员密码修改接口", notes = "系统管理员密码修改接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_USER, operateDesc = "账号修改密码")
    @PostMapping("/update/pwd")
    public Result<FySystemUser> updatePwd(@RequestBody SystemLoginPwdUpdateVo systemLoginPwdUpdateVo) {
        return fySystemUserService.updatePwd(systemLoginPwdUpdateVo);
    }

    /**
     * 系统账号手机号修改接口
     *
     * @param systemUserMobileUpdateVo 系统账号手机号编辑参数
     * @return 操作结果
     */
    @ApiOperation(value = "系统账号手机号修改接口", notes = "系统账号手机号修改接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_USER, operateDesc = "账号修改手机号")
    @PostMapping("/update/mobile")
    public Result<FySystemUser> updateMobile(@RequestBody SystemUserMobileUpdateVo systemUserMobileUpdateVo) {
        return fySystemUserService.updateMobile(systemUserMobileUpdateVo);
    }

    /**
     * 系统账号邮箱修改接口
     *
     * @param systemUserEmailUpdateVo 系统账号邮箱编辑参数
     * @return 操作结果
     */
    @ApiOperation(value = "系统账号邮箱修改接口", notes = "系统账号邮箱修改接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_USER, operateDesc = "账号修改邮箱")
    @PostMapping("/update/email")
    public Result<FySystemUser> updateEmail(@RequestBody SystemUserEmailUpdateVo systemUserEmailUpdateVo) {
        return fySystemUserService.updateEmail(systemUserEmailUpdateVo);
    }

    /**
     * 系统账号个人信息修改接口
     *
     * @param fySystemUser 系统账号个人信息编辑参数
     * @return 操作结果
     */
    @ApiOperation(value = "系统账号个人信息修改接口", notes = "系统账号个人信息修改接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_USER, operateDesc = "账号个人信息修改")
    @PostMapping("/info/update")
    public Result<FySystemUser> updateUserInfo(@RequestBody FySystemUser fySystemUser) {
        return fySystemUserService.updateUserInfo(fySystemUser);
    }

    /**
     * 获取用户菜单列表接口
     *
     * @return 菜单列表
     */
    @ApiOperation(value = "获取用户菜单列表接口", notes = "获取用户菜单列表接口", httpMethod = "GET")
    @GetMapping("/menu/list")
    public Result<List<FySystemMenu>> getAllUsableMenuList() {
        return fySystemUserService.getUserMenuList();
    }
}
